package com.isky.visual.design.sqlparsefactory;

import com.isky.visual.constant.DbConstant;
import com.isky.visual.exception.GlobalException;
import com.isky.visual.result.CodeMsg;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author wenjs
 * sql parse
 */

public class SqlParseFactory {

    private static final Map<String,Class> dbClassMap = new HashMap<>();
    static {
        dbClassMap.put(DbConstant.MYSQL,MySqlParse.class);
        dbClassMap.put(DbConstant.ORACLE,OracleSqlParse.class);
        dbClassMap.put(DbConstant.SQL_SERVER,SQLServerSqlParse.class);
        dbClassMap.put(DbConstant.PRESTO,PrestoParse.class);
    }

    public static <T extends ISqlParse> T createParser(Class<T> classT) {
        try {
            return (T) classT.newInstance();
        } catch (InstantiationException e) {
            throw new GlobalException(CodeMsg.DATA_ERROR);
        } catch (IllegalAccessException e) {

        }
        return null;
    }

    public static <T extends ISqlParse> T createParser(String dbType) {
        Class classZ =dbClassMap.get(dbType);
        Objects.requireNonNull(classZ,"未知的数据库类型");

        return (T) createParser(classZ);
    }
}
